﻿'========================================================================
'功能:登入資料匯入維護作業
'作者:shihyu
'日期: 2011/06/13
'========================================================================
Imports System.Data
Partial Class WebForm_OtherData_LoginDataMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName

            If Not ChkProgramRight(1) Then '查詢
                Me.btnQuery.Visible = False
            End If


            If Not ChkProgramRight(2) Then '編輯
                lblfileupload.Visible = False
                fuImport.Visible = False
                btnImport.ValidationGroup = False
            End If
        End If
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.btnImport)
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.gvLoginData.DataBind()

    End Sub

    Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
        If Not fuImport.HasFile Then
            ShowMessage("請選擇上傳的檔案")
        ElseIf IO.Path.GetExtension(fuImport.PostedFile.FileName).ToUpper() <> ".XLS" AndAlso IO.Path.GetExtension(fuImport.PostedFile.FileName).ToUpper() <> ".XLSX" Then
            ShowMessage("匯入失敗!!請選擇副檔名(xls、xlsx)檔案!!")
        End If
        '上傳暫存檔
        Dim strGuid As Guid = Guid.NewGuid()
        Dim newName As String = Server.MapPath("~/") & "Imports\" & strGuid.ToString() & IO.Path.GetExtension(fuImport.PostedFile.FileName).ToUpper()
        fuImport.PostedFile.SaveAs(newName)


        Dim ExcelImportCheck As New ExcelImportCheck()
        Dim dt As New DataTable

        '檢查工作表名稱
        If ExcelImportCheck.LoadData(dt, newName, "Sheet1",true) = False Then
            Return
        End If

        '重新命名需要使用的欄位
        For i As Integer = 0 To dt.Columns.Count - 1
            Select Case i
                Case 0
                    dt.Columns(0).ColumnName = "Site"
                Case 1
                    dt.Columns(1).ColumnName = "ComputerName"
                Case 2
                    dt.Columns(2).ColumnName = "IPAddress"
                Case 3
                    dt.Columns(3).ColumnName = "MACAddress"
                Case 5
                    dt.Columns(5).ColumnName = "LoginID"
                Case 11
                    dt.Columns(11).ColumnName = "LoginDate"
            End Select
        Next


        dt.Columns.Add("IPCount")
        dt.Columns.Add("MACCount")

        Dim bbl As AssetManage.BBL.LoginDataBBL = New AssetManage.BBL.LoginDataBBL()
        '處理excel資料
        For i As Integer = 0 To dt.Rows.Count - 1

            dt.Rows(i)("Site") = dt.Rows(i)("Site").ToString().ToUpper().Replace("-SITE", "")

            dt.Rows(i)("IPAddress") = dt.Rows(i)("IPAddress").ToString().Replace("""", "")
            If dt.Rows(i)("IPAddress").ToString.Contains("0.0.0.0,") Then
                dt.Rows(i)("IPAddress") = dt.Rows(i)("IPAddress").ToString.Replace("0.0.0.0,", "")
            ElseIf dt.Rows(i)("IPAddress").ToString.Contains(",0.0.0.0") Then
                dt.Rows(i)("IPAddress") = dt.Rows(i)("IPAddress").ToString.Replace(",0.0.0.0", "")
            End If

            If dt.Rows(i)("IPAddress").ToString.Contains(",") Then
                dt.Rows(i)("IPCount") = dt.Rows(i)("IPAddress").ToString.Split(",").Length
            Else
                dt.Rows(i)("IPCount") = 1
            End If

            dt.Rows(i)("MACAddress") = dt.Rows(i)("MACAddress").ToString().Replace("""", "")
            If dt.Rows(i)("MACAddress").ToString.Contains("00:00:00:00:00:00,") Then
                dt.Rows(i)("MACAddress") = dt.Rows(i)("MACAddress").ToString.Replace("00:00:00:00:00:00,", "")
            ElseIf dt.Rows(i)("MACAddress").ToString.Contains(",00:00:00:00:00:00") Then
                dt.Rows(i)("MACAddress") = dt.Rows(i)("MACAddress").ToString.Replace(",00:00:00:00:00:00", "")
            End If

            If dt.Rows(i)("MACAddress").ToString.Contains(",") Then
                dt.Rows(i)("MACCount") = dt.Rows(i)("MACAddress").ToString.Split(",").Length
            Else
                dt.Rows(i)("MACCount") = 1
            End If

        Next

      
        Dim j As Integer
        For i As Integer = 0 To dt.Rows.Count - 1
            j = 0
            If dt.Rows(i)("MACCount") > dt.Rows(i)("IPCount") Then
                For Each strMac As String In dt.Rows(i)("MACAddress").ToString().Split(",")
                    If dt.Rows(i)("IPCount") > 1 Then
                        If j = dt.Rows(i)("IPCount") Then
                            j = j - 1
                        ElseIf j = 0 Then
                            j = 0
                        Else
                            j = j + 1
                        End If
                        bbl.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Split(",")(j).Trim(), strMac.Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), hiUID.Value, dt.Rows(i)("ComputerName").ToString().Trim(), dbName)

                    Else
                        bbl.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Trim(), strMac.Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), hiUID.Value, dt.Rows(i)("ComputerName").ToString().Trim(), dbName)
                    End If

                Next
            ElseIf dt.Rows(i)("IPCount") > dt.Rows(i)("MACCount") Then
                For Each strIP As String In dt.Rows(i)("IPAddress").ToString().Split(",")

                    If dt.Rows(i)("MACCount") > 1 Then
                        If j = dt.Rows(i)("MACCount") Then
                            j = j - 1
                        ElseIf j = 0 Then
                            j = 0
                        Else
                            j = j + 1
                        End If
                        bbl.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), strIP.Trim(), dt.Rows(i)("MACAddress").ToString().Split(",")(j).Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), hiUID.Value, dt.Rows(i)("ComputerName").ToString().Trim(), dbName)
                    Else
                        bbl.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), strIP.Trim(), dt.Rows(i)("MACAddress").ToString().Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), hiUID.Value, dt.Rows(i)("ComputerName").ToString().Trim(), dbName)
                    End If


                Next

            ElseIf dt.Rows(i)("MACCount") = 1 AndAlso dt.Rows(i)("IPCount") = 1 Then
                bbl.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Trim(), dt.Rows(i)("MACAddress").ToString().Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), hiUID.Value, dt.Rows(i)("ComputerName").ToString().Trim(), dbName)
            ElseIf dt.Rows(i)("MACCount") = dt.Rows(i)("IPCount") AndAlso dt.Rows(i)("MACCount") > 1 Then

                For Each strMac As String In dt.Rows(i)("MACAddress").ToString().Split(",")
                    bbl.InsertLoginData(dt.Rows(i)("Site").ToString().Trim(), dt.Rows(i)("IPAddress").ToString().Split(",")(j).Trim(), strMac.Trim(), dt.Rows(i)("LoginID").ToString().Trim(), dt.Rows(i)("LoginDate").ToString().Trim(), hiUID.Value, dt.Rows(i)("ComputerName").ToString().Trim(), dbName)
                    j = j + 1
                Next
            End If
        Next

        ShowMessage(Me.GetLocalResourceObject("ImportSuccessful"))
        Me.gvLoginData.DataBind()
    End Sub

    Protected Sub gvLoginData_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvLoginData.RowCommand

        Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
        If e.CommandName = "LocShow" Then
            IPData1.IPAddress = Me.gvLoginData.DataKeys(row.RowIndex).Item("IPAddress").ToString()
            IPData1.Visible = True
            IPData1.InitAndShow()
        End If
    End Sub

    Protected Sub txtAsset_serialNotChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAsset_serialNo.TextChanged
        Dim dt As DataTable
        Dim dao As AssetManage.DAO.AssetDAO = New AssetManage.DAO.AssetDAO(dbName)
        dt = dao.GetAssetData("", "", txtAsset_serialNo.Text, "", "", "", "", "", "")
        If dt.Rows.Count > 1 Or dt.Rows.Count = 0 Then
            ShowMessageForAjax(btnImport, "Asset Data records more than 1 or no record ,so system can not get computer name and MAC automatically.")
            txtMAC.Text = ""
            txtMAC1.Text = ""
            txtComputerName.Text = ""
            Return
        Else
            txtMAC.Text = dt.Rows(0)("MAC1").ToString
            txtMAC1.Text = dt.Rows(0)("MAC2").ToString
            txtMAC.Text = dt.Rows(0)("MAC1").ToString
            txtComputerName.Text = dt.Rows(0)("ComputerName").ToString


        End If

    End Sub
End Class
